package com.tmall.tmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tmall.tmall.mapper.ShippingAddressMapper;
import com.tmall.tmall.pojo.Address;
import com.tmall.tmall.pojo.AddressRt;
import com.tmall.tmall.pojo.PageRt;
import com.tmall.tmall.pojo.User;
import com.tmall.tmall.service.ShippingAddressService;
import com.tmall.tmall.utils.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShippingAddressServiceImpl implements ShippingAddressService {
    @Autowired
    private ShippingAddressMapper shippingAddressMapper;

    @Override
    public void insert(Address address) {
        address.setCreateTime(LocalDateTime.now());
        address.setUid(ThreadLocalUtils.getUid());
        shippingAddressMapper.insert(address);
    }

    @Override
    public PageRt page(Integer pageNum, Integer pageSize, String name, String tel, String address, Integer uid) {
        PageHelper.startPage(pageNum, pageSize);
        List<AddressRt> list = shippingAddressMapper.page(name,tel,address,uid);
        PageInfo<AddressRt> pageInfo = new PageInfo<>(list);
        return new PageRt(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public void update(Address address) {
        shippingAddressMapper.update(address);
    }

    @Override
    public void delete(List<Integer> ids) {
        for (Integer id : ids) {
            Address address = Address.builder().id(id).deleteAt(LocalDateTime.now()).build();
            shippingAddressMapper.update(address);
        }
    }

    @Override
    public List<AddressRt> getById(Integer id) {
        return shippingAddressMapper.getById(id);
    }
}
